import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { Router } from "@angular/router";
import { LocalStorageService } from './local-storage.service';

@Injectable({
  providedIn: 'root'
})
export class RouteguardService implements CanActivate{

  currentUser:any;

  constructor(
    private stotage: LocalStorageService,
    private router: Router
  ) { }

  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean{
    // 返回值 true: 跳转到当前路由 false: 不跳转到当前路由
    // 当前路由名称
    var path = route.routeConfig.path;  
    // nextRoute: 设置需要路由守卫的路由集合
    const nextRoute = ['employee', 'departmentManager', 'viceGeneralManager', 'manager', 'admin'];
    // let isLogin = userModel.isLogin;  // 是否登录
    // 当前路由是nextRoute指定页时
    if (nextRoute.indexOf(path) >= 0) {
      if (this.stotage.getObject('currentUser')==null) {
        // 未登录，跳转到login
        this.router.navigate(['login']);
        return false;
      }else{
        this.currentUser = this.stotage.getObject('currentUser');
        if (this.currentUser.authority==nextRoute.indexOf(path)) {
          // 已登录，跳转到当前路由
          return true;
        }
        else{
          //权限不足
          this.router.navigate(['login']);
          return false;
        }
      }
    }
    // // 当前路由是login时 
    // if (path === 'login') {
    //   if (!isLogin) {
    //     // 未登录，跳转到当前路由
    //     return true;
    //   }else{
    //     // 已登录，跳转到home
    //     this.router.navigate(['home']);
    //     return false;
    //   }
    // }
  }
}
